package com.aaa.sbms.dao;



import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface DMDao {

    //加载数据
    @Select("<script>select USERID,USERNAME,SEX,FLOOR,ROOM,CHECKINDATE,STATE from checkins " +
            " where STATE=0 " +
            "<if test=\"USERID!=null and USERID!=''\"> and USERID like concat('%',#{USERID},'%') </if>" +
            "<if test=\"USERNAME!=null and USERNAME!=''\"> and USERNAME like concat('%',#{USERNAME},'%')  </if>" +
            "</script>")
    List<Map> list(Map map);

    //根据员工工号检查员工是否已入住
    @Select("select u.USERID,u.USERNAME,u.SEX, c.ROOM from users u left join checkins c on u.USERID=c.USERID where u.USERID=#{USERID}")
    List<Map> checkByUserId (Map map);

    //根据性别查询员工宿舍单元
    @Select("select FLOOR from houses where SEX=#{SEX} group by FLOOR")
    List<Map> floorList(Map map);

    //根据性别和楼层查询楼层房间信息
    @Select("select ROOM from houses where BED!=0 and FLOOR=#{floor} AND SEX=#{SEX} AND ROOM!=#{ROOM} group by ROOM")
    List<Map> roomList(Map map);

    //根据性别和楼层查询楼层房间信息
    @Select("select ROOM from houses where BED!=0 and FLOOR=#{floor} AND SEX=#{SEX} group by ROOM")
    List<Map> addRoomList(Map map);

    @Update("update houses set BED=BED+1 where SEX=#{SEX} and FLOOR=#{FLOOR} and ROOM=#{ROOM}")
    int addBedCount(Map map);

    @Update("update houses set BED=BED-1 where SEX=#{SEX} and FLOOR=#{floor} and ROOM=#{room}")
    int delBedCount(Map map);

    @Insert("<script> insert into checkins(USERID,USERNAME,SEX,FLOOR,ROOM,CHECKINDATE,HOUSEID) \n" +
            "VALUES(#{USERID},#{USERNAME},#{SEX},#{floor},#{room},NOW(),#{HOUSEID}" +
            ")</script>")
    int insert(Map map);

    //修改住房信息
    @Update("update checkins  set USERNAME=#{USERNAME},SEX=#{SEX},FLOOR=#{floor},ROOM=#{room},STATE=#{STATE}," +
            "CHECKINDATE=#{updateTime} where USERID=#{USERID}")
    int update(Map map);

    @Update("update checkins  set STATE=2 where USERID=#{USERID}")
    Integer updateIng(Integer USERID);
}
